<template>
    <view :class="theme_view">
        <block v-if="data.length > 0">
            <scroll-view :scroll-y="true" class="scroll-box" @scrolltolower="scroll_lower" lower-threshold="60">
                <view class="page-bottom-fixed">
                    <view v-for="(item, index) in data" class="bg-white spacing-mb business-card" :key="index">
                        <view class="padding-main flex-row">
                            <view v-if="item.cover" class="padding-right-main">
                                <image class="classinfo-img radius" :src="item.cover" mode="aspectFit"></image>
                            </view>
                            <view class="flex-1 flex-width">
                                <view v-if="item.status == 1" :data-value="'/pages/fcc/classinfo/detail/detail?id=' + item.id" @tap="url_event" class="multi-text text-size fw-b margin-bottom-xs cr-blue">【{{ item.goods_name }}】<text v-if="item.title != ''">{{item.title}}</text></view>
								<view v-else class="multi-text text-size fw-b margin-bottom-xs">【{{ item.goods_name }}】<text v-if="item.title != ''">{{item.title}}</text></view>
                                <view class="text-size-sm cr-grey-9 margin-bottom-sm">发布时间：{{ item.add_time }}</view>
								<view v-if="item.over_time > 0" class="text-size-sm cr-grey-9 margin-bottom-sm" :class="(item.is_over == 1 ? 'cr-red' : '')">截止时间：{{ item.over_time_all }}</view>
								<!--<view v-if="item.infoendtime_old > 0" class="text-size-sm cr-grey-9 margin-bottom-sm" :class="(item.info_status == 1 ? 'cr-red' : '')">到期时间：{{ item.infoendtime }}</view>-->
								<view>
									<text class="badge" :class="item.status == 0 ? 'info' : (item.status === 1 ? 'success' : 'error')">{{ item.status_name }}</text>
									<text class="badge margin-left-xs" :class="item.pay_status == 0 ? 'info' : (item.pay_status === 1 ? 'success' : 'error')">{{ item.pay_status_name }}</text>
									<text v-if="item.is_over == 1" class="badge margin-left-xs success">已完成</text>
									<text v-if="item.info_status == 1" class="badge margin-left-xs error">{{ item.info_status_name }}</text>
								</view>
                                
								<view v-if="item.status == 2" class="cr-main margin-bottom">拒绝理由：{{item.reason}}</view>
								<view class="cr-base text-size-sm multi-text margin-top-sm">{{ item.describe }}</view>
                            </view>
                        </view>
						<view class="item-operation tr plr-11">
							<button v-if="item.info_status == 0" class="round bg-white cr-blue br-blue margin-bottom-main" type="default" size="mini" :data-value="'/pages/fcc/classinfo/form/form?gid='+item.gid+'&id=' + item.id" @tap="url_event" hover-class="none">{{$t('common.edit')}}</button>
							<button v-else class="round bg-white cr-blue br-blue margin-bottom-main" type="default" size="mini" :data-value="'/pages/fcc/classinfo/form/form?resend=1&gid='+item.gid+'&id=' + item.id" @tap="url_event" hover-class="none">重发</button>
							<!--<button class="round bg-white cr-yellow br-yellow margin-bottom-main" type="default" size="mini" :data-id="item.id" @tap="del_event" hover-class="none">{{$t('common.del')}}</button>-->
							<button v-if="(item.order_info || null) != null && item.pay_status == 0" class="round bg-white cr-green br-green margin-bottom-main" type="default" size="mini" @tap="pay_event" :data-value="item.order_info.id" :data-index="index" :data-price="item.order_info.total_price" :data-payment="item.order_info.payment_id" :data-currency-symbol="(item.order_info || null) != null ? item.order_info.currency_data.currency_symbol : currency_symbol" hover-class="none">{{$t('order.order.1i873j')}}</button>
							<button v-if="item.status == 1 && item.is_over == 0" class="round bg-white cr-red br-red margin-bottom-main" type="default" size="mini" @tap="over_event" :data-id="item.id" :data-index="index" hover-class="none">完成</button>
							<button v-if="item.status == 1 && item.info_status == 0" class="round bg-white cr-yellow br-yellow margin-bottom-main" type="default" size="mini" @tap="finish_event" :data-id="item.id" :data-index="index" hover-class="none">下架</button>
						</view> 
                    </view>
                    <!-- 结尾 -->
                    <component-bottom-line :propStatus="data_bottom_line_status"></component-bottom-line>
                </view>
            </scroll-view>
        </block>

        <block v-if="data.length == 0 && data_list_loding_status != 3">
            <!-- 提示信息 -->
            <component-no-data :propStatus="data_list_loding_status" :propMsg="data_list_loding_msg"></component-no-data>
        </block>
		
		<!-- 支付组件 -->
		<component-payment :diyTheme="diytheme" 
		    ref="payment"
		    :propCurrencySymbol="currency_symbol"
		    :propPayUrl="pay_url"
		    :propQrcodeUrl="qrcode_url"
		    :propPaymentList="payment_list"
		    propPayDataKey="ids"
		    :propTempPayValue="temp_pay_value"
		    :propTempPayIndex="temp_pay_index"
		    :propPaymentId="payment_id"
		    :propDefaultPaymentId="default_payment_id"
		    :propPayPrice="pay_price"
		    :propIsShowPayment="is_show_payment_popup"
		    @close-payment-popup="payment_popup_event_close"
		    @pay-success="order_item_pay_success_handle"
		></component-payment>
		
		<component-classinfo-footer :propBaseData="data_base"></component-classinfo-footer>
    </view>
</template>
<script>
    const app = getApp();
    import componentNoData from '@/components/no-data/no-data';
    import componentBottomLine from '@/components/bottom-line/bottom-line';
	import componentPayment from '@/components/payment/payment';
	import {system_base_info} from "@/common/js/common/common";
	import componentClassinfoFooter from '../public/footer';
	import {classinfo_base_info} from '@/pages/fcc/classinfo/public/common.js';
    export default {
        data() {
            return {
                theme_view: app.globalData.get_theme_value_view(),
                theme_color: app.globalData.get_theme_color(),
				currency_symbol: app.globalData.currency_symbol(),
                data_bottom_line_status: false,
                data_list_loding_status: 1,
                data_list_loding_msg: '',
                data: [],
                data_is_loading: 0,
                data_total: 0,
                data_page_total: 0,
                data_page: 1,
				data_base: null,
				diytheme: null,
				// 支付弹窗参数
				pay_url: app.globalData.get_request_url('pay', 'index', 'classinfo'),
				qrcode_url: app.globalData.get_request_url('paycheck', 'index', 'classinfo'),
				payment_list: [],
				temp_pay_index: 0,
				temp_pay_value: '',
				is_show_payment_popup: false,
				pay_price: 0,
				payment_id: 0,
				// 支付失败跳转的页面
				to_fail_page: '/pages/fcc/classinfo/user-list/user-list',
				to_appoint_page: '/pages/fcc/classinfo/user-list/user-list',
				default_payment_id: 0,
            };
        },
        components: {
            componentNoData,
            componentBottomLine,
			componentPayment,
			componentClassinfoFooter
        },

        onLoad(params) {
            
        },

        onShow() {
			app.globalData.page_event_onshow_handle();
			
            // 数据加载
            this.init();
        },

        // 下拉刷新
        onPullDownRefresh() {
            this.setData({
                data_page: 1,
            });
            this.get_data_list(1);
        },

        // 页面销毁时执行
        onUnload: function () {},

        methods: {
			// 加载之前
			init() {
				var user = app.globalData.get_user_info(this, 'init');
				if (user != false) {
				    // 先获取语言基础信息
				    system_base_info()
				    .then(data=>{
				    	this.init_before(data)
				    	.then(()=>{
							classinfo_base_info()
							.then(data=>{
								this.sys_init(data);
								this.get_data_list(1);
							});
				    	});
				    });
				} else {
				    // 提示错误
				    this.setData({
				        data_list_loding_status: 2,
				        data_list_loding_msg: this.$t('form.form.8l3ul5'),
				    });
				}
			},
			// 设置系统基础参数
			sys_init(data){
				this.setData({
					data_base: data
				});
			},
			// 设置本页参数
			init_before(data){
				var self = this;
				return new Promise((resolve, reject) => {
					self.setData({
						lang_info: data.system_lang_info,
						diytheme: data.diytheme,
						currency_symbol: app.globalData.get_config('currency_symbol'),
					});
					resolve();
				});
			},
            get_data_list(is_mandatory) {
                // 分页是否还有数据
                if ((is_mandatory || 0) == 0) {
                    if (this.data_bottom_line_status == true) {
                        uni.stopPullDownRefresh();
                        return false;
                    }
                }
                // 是否加载中
                if (this.data_is_loading == 1) {
                    return false;
                }
                this.setData({
                    data_is_loading: 1,
                    data_list_loding_status: 1,
                });
                // 加载loding
                uni.showLoading({
                    title: this.$t('common.loading_in_text'),
                });
                uni.request({
                    url: app.globalData.get_request_url('index', 'info', 'classinfo'),
                    method: 'POST',
                    success: (res) => {console.log(res.data);
                        uni.hideLoading();
                        uni.stopPullDownRefresh();
                        if (res.data.code == 0) {
                            var data = res.data.data;
                            if (data.data.length > 0) {
                                if (this.data_page <= 1) {
                                    var temp_data_list = data.data;
                                } else {
                                    var temp_data_list = this.data_list || [];
                                    var temp_data = data.data;
                                    for (var i in temp_data) {
                                        temp_data_list.push(temp_data[i]);
                                    }
                                }
                                this.setData({
                                    data: temp_data_list,
                                    data_total: data.total,
                                    data_page_total: data.page_total,
                                    data_list_loding_status: 3,
                                    data_list_loding_msg: '',
                                    data_page: this.data_page + 1,
                                    data_is_loading: 0,
									payment_list: data.payment_list || [],
									default_payment_id: data.default_payment_id || 0,
                                });

                                // 是否还有数据
                                this.setData({
                                    data_bottom_line_status: this.data_page > 1 && this.data_page > this.data_page_total,
                                });
                            } else {
                                this.setData({
                                    data_list_loding_status: 0,
                                    data_total: 0,
                                    data_is_loading: 0,
                                });
                                if (this.data_page <= 1) {
                                    this.setData({
                                        data: [],
                                        data_bottom_line_status: false,
                                    });
                                }
                            }
                        } else {
                            this.setData({
                                data_list_loding_status: 0,
                                data_list_loding_msg: res.data.msg,
                                data_is_loading: 0,
                            });
                            app.globalData.showToast(res.data.msg);
                        }
                    },
                    fail: () => {
                        uni.hideLoading();
                        uni.stopPullDownRefresh();
                        this.setData({
                            data_list_loding_status: 2,
                            data_list_loding_msg: this.$t('common.internet_error_tips'),
                            data_is_loading: 0,
                        });
                        app.globalData.showToast(this.$t('common.internet_error_tips'));
                    },
                });
            },
			
			// 支付
			pay_event(e) {
			    // 参数
			    var index = e.currentTarget.dataset.index || 0;
			    var payment_id = e.currentTarget.dataset.payment || 0;
			    // 设置支付参数
			    this.setData({
			        is_show_payment_popup: true,
			        payment_list: this.payment_list,
			        payment_currency_symbol: e.currentTarget.dataset.currencySymbol,
			        temp_pay_value: e.currentTarget.dataset.value,
			        temp_pay_index: index,
			        payment_id: payment_id,
			        pay_price: e.currentTarget.dataset.price,
			        order_select_ids: [],
			    });
			},
			
			// 支付弹窗关闭
			payment_popup_event_close(e) {
			    this.setData({
			        is_show_payment_popup: false,
			    });
			},
			
			// 支付成功数据设置
			// 订单完成回调
			order_item_pay_success_handle(data) {
			    var order_ids_arr = data.order_id.toString().split(',');
			    var temp_data_list = this.data;
			    for (var i in temp_data_list) {
			        if (order_ids_arr.indexOf(temp_data_list[i]['id'].toString()) != -1) {
			            temp_data_list[i]['pay_status'] = 1;
			            temp_data_list[i]['pay_status_name'] = this.$t('order.order.s8g966');
			        }
			    }
			    this.setData({
			        data: temp_data_list,
			    });
			},

            // url事件
            url_event(e) {
                app.globalData.url_event(e);
            },
			
			// 下架
			finish_event(e) {
			    // 是否再次确认
			    if (e.alert_status != 0 && e.alert_status != 1) {
			        app.globalData.alert({
			            msg: '确定下架这条信息吗？',
			            is_show_cancel: 1,
			            object: this,
			            params: { id: e.currentTarget.dataset.id },
			            method: 'finish_event',
			        });
			        return false;
			    }
			    if (e.alert_status == 1) {
			        // 加载loding
			        uni.showLoading({
			            title: this.$t('common.loading_in_text'),
			        });
			        uni.request({
			            url: app.globalData.get_request_url('statusupdate', 'info', 'classinfo'),
			            method: 'POST',
			            data: { id: e.id, field: 'info_status', state: 1 },
			            success: (res) => {
			                uni.hideLoading();
			                if (res.data.code == 0) {
			                    var temp_data_list = this.data;
			                    for (var i in temp_data_list) {
									if (e.id == temp_data_list[i]['id']) {
										temp_data_list[i]['info_status'] = 1;
									}
			                    }
			                    this.setData({
			                        data: temp_data_list,
			                    });
			                    app.globalData.showToast(res.data.msg, 'success');
			                } else {
			                    if (app.globalData.is_login_check(res.data)) {
			                        app.globalData.showToast(res.data.msg);
			                    } else {
			                        app.globalData.showToast(this.$t('common.sub_error_retry_tips'));
			                    }
			                }
			            },
			            fail: () => {
			                uni.hideLoading();
			                app.globalData.showToast(this.$t('common.internet_error_tips'));
			            },
			        });
			    }
			},
			
			// 完成
			over_event(e) {console.log(e);
			    app.globalData.alert({
			        msg: '确定已完成这条信息吗？',
			        is_show_cancel: 1,
			        object: this,
			        params: { id: e.currentTarget.dataset.id },
			        method: 'over_event_true',
			    });
			},
			over_event_true(params){
			    if(params['alert_status'] == 1) {
			        // 加载loding
			        uni.showLoading({
			            title: this.$t('common.loading_in_text'),
			        });
			        uni.request({
			            url: app.globalData.get_request_url('statusupdate', 'info', 'classinfo'),
			            method: 'POST',
			            data: { id: params.id, field: 'is_over', state: 1 },
			            success: (res) => {
			                uni.hideLoading();
			                if (res.data.code == 0) {
			                    var temp_data_list = this.data;
			                    for (var i in temp_data_list) {
									if (params.id == temp_data_list[i]['id']) {
										temp_data_list[i]['is_over'] = 1;
									}
			                    }
			                    this.setData({
			                        data: temp_data_list,
			                    });
			                    app.globalData.showToast(res.data.msg, 'success');
			                } else {
			                    if (app.globalData.is_login_check(res.data)) {
			                        app.globalData.showToast(res.data.msg);
			                    } else {
			                        app.globalData.showToast(this.$t('common.sub_error_retry_tips'));
			                    }
			                }
			            },
			            fail: () => {
			                uni.hideLoading();
			                app.globalData.showToast(this.$t('common.internet_error_tips'));
			            },
			        });
			    }
			},
            // 删除classinfo
            del_event(e) {
                // 是否再次确认
                if (e.alert_status != 0 && e.alert_status != 1) {
                    app.globalData.alert({
                        msg: '确定删除这条信息吗？',
                        is_show_cancel: 1,
                        object: this,
                        params: { id: e.currentTarget.dataset.id },
                        method: 'del_event',
                    });
                    return false;
                }
                if (e.alert_status == 1) {
                    // 加载loding
                    uni.showLoading({
                        title: this.$t('common.loading_in_text'),
                    });
                    uni.request({
                        url: app.globalData.get_request_url('delete', 'info', 'classinfo'),
                        method: 'POST',
                        data: { ids: e.id },
                        success: (res) => {
                            uni.hideLoading();
                            if (res.data.code == 0) {
                                var temp_list = [];
                                var temp_data_list = this.data;
                                for (var i in temp_data_list) {
                                    if (e.id.indexOf(temp_data_list[i]['id']) == -1) {
                                        temp_list.push(temp_data_list[i]);
                                    }
                                }
                                this.setData({
                                    data: temp_list,
                                });
                                app.globalData.showToast(this.$t('user-list.user-list.kpn3fp'), 'success');
                            } else {
                                if (app.globalData.is_login_check(res.data)) {
                                    app.globalData.showToast(this.$t('user-list.user-list.649j60'));
                                } else {
                                    app.globalData.showToast(this.$t('common.sub_error_retry_tips'));
                                }
                            }
                        },
                        fail: () => {
                            uni.hideLoading();
                            app.globalData.showToast(this.$t('common.internet_error_tips'));
                        },
                    });
                }
            },
            // 滚动加载
            scroll_lower(e) {
                this.get_data_list();
            },
        },
    };
</script>
<style>
    @import './user-list.css';
</style>
